/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package clases;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 *
 * @author Juan
 */
public class SVConexion extends Thread {

    private Socket s;
    private DataInputStream dis;
    private DataOutputStream dos;
    private String nick;

    public SVConexion(Socket s) {
        try {
            this.s = s;
            dis = new DataInputStream(s.getInputStream());
            dos = new DataOutputStream(s.getOutputStream());
            start();
        } catch (Exception e) {
        }
    }

    public String getNick() {
        return nick;
    }

    public void run() {
        while (true) {
            try {
                int nCodigo = dis.readInt();
                String mTrama = dis.readUTF();
                Sql sql = new Sql();
                switch (nCodigo) {
                    case 1:
                        nick = mTrama;
                        SVGestorConexiones.getInstancia().enviarTrama(this, nCodigo, mTrama);
                        break;
                    case 2:
                        String a = sql.EntradaA(mTrama, Integer.parseInt(nick));
                        mTrama=a;
                        SVGestorConexiones.getInstancia().enviarTrama(this, nCodigo, mTrama);
                        break;
                    case 3:
                        String b=sql.SalidaA(Integer.parseInt(nick));
                        mTrama=b;
                        SVGestorConexiones.getInstancia().enviarTrama(this, nCodigo, mTrama);
                        break;
                    case 4:
                        String c=sql.EmtradaM(mTrama, Integer.parseInt(nick));
                        mTrama=c;
                        SVGestorConexiones.getInstancia().enviarTrama(this, nCodigo, mTrama);
                        break;
                    case 5:
                        String d=sql.SalidaM(Integer.parseInt(nick));
                        mTrama=d;
                        SVGestorConexiones.getInstancia().enviarTrama(this, nCodigo, mTrama);
                        break;
                    case 6:
                        SVGestorConexiones.getInstancia().enviarMensaje(nCodigo, mTrama);
                        break;
                    case 7:
                        SVGestorConexiones.getInstancia().enviarMensaje(nCodigo, mTrama);
                        break;
                    case 8:
                        SVGestorConexiones.getInstancia().desconecta(this);
                        break;
                }
            } catch (IOException | NumberFormatException e) {
            }
        }

    }

    public void enviarTrama(int nCodigo, String mTrama) {
        try {
            dos.writeInt(nCodigo);
            dos.writeUTF(mTrama);
        } catch (Exception e) {
        }
    }
}
